/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Plugins;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Settings;
using System.Globalization;
using MyPhotoIndex.Interfaces.Plugins;
using System.Collections.ObjectModel;

namespace MyPhotoIndex
{
    public partial class PluginSelectionForm : FormTemplate
    {
        private Type        m_pluginType;
        private ImageList   m_imageList = new ImageList();

        public PluginSelectionForm(Type pluginType)
        {
            InitializeComponent();
            m_pluginType = pluginType;
            m_imageList.ImageSize = new Size(16, 16);
            m_imageList.ColorDepth = ColorDepth.Depth32Bit;
            m_imageList.TransparentColor = Color.White;

            customListBox1.MouseDoubleClick += new MouseEventHandler(customListBox1_MouseDoubleClick);
        }

        private void PluginSelection_Load(object sender, EventArgs e)
        {
            this.CenterToParent();

            String defaultMailPlugin = ApplicationSettings.Default.DefaultMailPlugin;

            ReadOnlyCollection<IPlugin> plugins = Manager.GetPlugins<IPlugin>();
            foreach (IPlugin plugin in plugins)
            {
                if (plugin.GetType().GetInterface(m_pluginType.Name) != null)
                {
                    Image pluginIcon = null;
                    try
                    {
                        pluginIcon = ((ISharePlugin)plugin).Icon;
                    }
                    catch { }

                    if (pluginIcon != null)
                    {
                        m_imageList.Images.Add(pluginIcon);
                    }

                    customListBox1.Items.Add(plugin);

                    if (String.Compare(plugin.GetType().FullName, defaultMailPlugin, true, CultureInfo.InvariantCulture) == 0)
                    {
                        customListBox1.SelectedItem = plugin;
                    }
                }
            }

            if (customListBox1.SelectedItem == null)
            {
                customListBox1.SelectedIndex = 0;
            }

            customListBox1.ImageList = m_imageList;
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            ApplicationSettings.Default.DefaultMailPlugin = customListBox1.SelectedItem.GetType().FullName;
            ApplicationSettings.Default.Save();

            base.OkButton_Click(sender, e);
        }

        void customListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OkButton_Click(sender, e);
        }
    }
}